package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;

@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);   //参数无效
        }
        dto.checkParam();   //检查分页查询
        //2.封装查询条件
        Page pageRequest = new Page<>(dto.getPage(), dto.getSize());    //分页条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            //根据名称模糊查询 "username"
            queryWrapper.like(AdChannel::getName, dto.getName());
        }
        //3.分页查询
        //分页条件、查询条件
        IPage<AdChannel> pageResult = page(pageRequest, queryWrapper);
        //4.封装响应结果返回
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal());
        responseResult.setData(pageResult.getRecords());    //分页集合数据
        return responseResult;
    }


    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.检查参数
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.保存
        adChannel.setCreatedTime(new Date());
        adChannel.setIsDefault(false);
        save(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1.检查参数
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.修改
        updateById(adChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //先查询是否存在
        AdChannel adChannel = getById(id);
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //2.删除频道
        removeById(id);

        //自己主动抛出异常，参数错误，id不能小于10
        if (id < 10) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "ID不能小于10");
            throw new CustomException(ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "id不能小于10"));
        }

        //3.返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }


}
